package com.imooc.common.thread;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by deng_wb on 2019/8/17
 */
public class WorkStealingPoolTest {

    @SuppressWarnings("all")
    public static void main(String[] args)throws Exception {
        ExecutorService service = Executors.newWorkStealingPool(); // 工作窃取
        System.out.println(Runtime.getRuntime().availableProcessors());

        service.execute(new R(1000));
        service.execute(new R(2000));
        service.execute(new R(2000));
        service.execute(new R(2000));
        service.execute(new R(2000));
        service.execute(new R(2000));
        service.execute(new R(2000)); //daemon
        service.execute(new R(2000));
        service.execute(new R(2000));
        // 由于产生的是精灵线程(守护线程 后台线程),主线程不阻塞的话,看不到输出
        System.in.read();
    }


    static class R implements Runnable {

        int time;
        R(int time){
            this.time = time;
        }

        @Override
        public void run() {
            ThreadUtil.sleep(time);
            System.out.println(time+"   "+Thread.currentThread().getName());
        }
    }
}
